package com.android.qjpmc.application_module.activity;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.android.qjpmc.R;
import com.android.qjpmc.application_module.contract.HistoryDataContract;
import com.android.qjpmc.application_module.station.fragment.HisCurveFragment;
import com.android.qjpmc.application_module.station.fragment.ReportDataFragment;
import com.android.qjpmc.application_module.viewModel.HistoryDataViewModel;
import com.android.qjpmc.bean.StationDataBean;
import com.android.qjpmc.bean.TabListBean;
import com.android.qjpmc.data_module.PwrDataModule;
import com.android.qjpmc.data_module.PwrStation;
import com.android.qjpmc.databinding.ActivityStationHistoryDataBinding;
import com.android.qjpmc.manager.FragmentManager;
import com.android.qjpmc.manager.IntentManager;
import com.android.qjpmc.widget.base.BaseCommonActivity;
import com.android.qjpmc.widget.base.BaseFragmentAdapter;
import com.android.qjpmc.widget.base.BasePageManageActivity;
import com.android.qjpmc.widget.mvvm.factory.CreateViewModel;
import com.android.qjpmc.widget.utils.UIUtils;
import com.android.qjpmc.widget.view.tablayout.TabLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager.widget.ViewPager;

@CreateViewModel(HistoryDataViewModel.class)
public class HistoryDataActivity extends BasePageManageActivity<HistoryDataViewModel, ActivityStationHistoryDataBinding> implements HistoryDataContract.View {

    private String stationId;
    private String stationName;

    @Override
    public int getLayoutId() {
        return R.layout.activity_station_history_data;
    }

    private List<TabListBean> tabList;
    private List<Fragment> fragmentList;
    private List<String> titleList;
    private BaseFragmentAdapter adapter;
    ReportDataFragment m_report_fragment = null;

    @Override
    public void initData() {
        super.initData();
        requestNetData();
    }

    @Override
    protected void requestNetData() {
        mViewModel.getGroupListOfStation(stationId);
    }

    @Override
    protected View getPageManagerView() {
        return mBinding.llContent;
    }

    @Override
    public void initView() {
        super.initView();
        stationId = getIntent().getStringExtra("stationId");
        stationName = getIntent().getStringExtra("stationName");

    }


    @Override
    public void initEvent() {
        super.initEvent();
        mBinding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mBinding.tabLayout.getTabAt(position).select();

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabTextView(tab, true);
                if (adapter != null && adapter.getCount() > 0 && mBinding.tabLayout.getTabCount() > 0)
                    mBinding.viewPager.setCurrentItem(tab.getPosition());

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabTextView(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

    }

    public void updateTabTextView(TabLayout.Tab tabSelect, boolean isSelect) {
        View bgView = tabSelect.getCustomView().findViewById(R.id.view_bg);
        TextView tvTitle = tabSelect.getCustomView().findViewById(R.id.tv_title);
        tvTitle.setTextColor(UIUtils.getColor(isSelect ? R.color.color_fefefe : R.color.gray_66));
        bgView.setBackgroundResource(isSelect ? R.drawable.shape_color_244395_18dp : R.drawable.shape_color_f0f0f0_15dp);
    }

    private View getTabView(final TabListBean tabListBean) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_station, null);
        TextView textView = view.findViewById(R.id.tv_title);
        textView.setText(tabListBean.title);
        return view;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mBinding.tabLayout.getSelectedTabPosition() == 0) {
            if (m_report_fragment != null) {
                m_report_fragment.UpdateSelectResult();
            }
        }
    }

    @Override
    public void showError(String message, int code) {
        mPageManage.showError(message);
    }

    @Override
    public void showLoading(String message) {
        mPageManage.showLoading(message);

    }

    @Override
    public void showContent(StationDataBean data) {
        mPageManage.showContent();
        data.companyName= stationName;
        data.companyId= stationId;

        tabList = new ArrayList<TabListBean>();
        tabList.add(new TabListBean(1, "报表数据"));
        tabList.add(new TabListBean(2, "历史曲线"));
        tabList.add(new TabListBean(3, "故障录波"));
        fragmentList = new ArrayList<>();
        titleList = new ArrayList<>();

        for (int i = 0; i < tabList.size(); i++) {
            TabListBean tabListBean = tabList.get(i);
            titleList.add(tabListBean.title);
            if (tabListBean.id == 1) {
                m_report_fragment = (ReportDataFragment) FragmentManager.getReportDataFragment(tabListBean.id);
                m_report_fragment.initStationData(stationId, data);
                fragmentList.add(m_report_fragment);
            } else if (tabListBean.id == 2) {
                HisCurveFragment hisCurveFragment = FragmentManager.getHisCurveFragment(tabListBean.id);
                fragmentList.add(hisCurveFragment);
                hisCurveFragment.initStationData(stationId, data);

            } else if (tabListBean.id == 3) {
                fragmentList.add(FragmentManager.getFaultWaveFragment(tabListBean.id));
            }
            final TabLayout.Tab tab = mBinding.tabLayout.newTab();
            tab.setCustomView(getTabView(tabListBean));
            final View tabView = (View) tab.getCustomView().getParent();
            tabView.setTag(i);
            mBinding.tabLayout.addTab(tab);
        }
        updateTabTextView(mBinding.tabLayout.getTabAt(0), true);
        adapter = new BaseFragmentAdapter(getSupportFragmentManager(), titleList, fragmentList, 1);
        mBinding.viewPager.setAdapter(adapter);

        PwrStation ThePwrStation = PwrDataModule.getInstance().GetCurrentStationNode();
        if (ThePwrStation != null) {
            mBinding.toolbar.setTitleText(ThePwrStation.getM_Name() + "·历史数据");// getToolsBar().setTitle("HLLL");
        } else {
            mBinding.toolbar.setTitleText(TextUtils.isEmpty(stationName) ? "未知站点" : stationName);
        }

    }

    @Override
    public void showEmpty(String message) {
        mPageManage.showEmpty(message);
    }
}
